/*
These files are distributed under the terms of the GNU GPLv2, 
the GNU General Public version 2. See LICENSE.txt
*/
package fileCommodore.model;

import java.util.*;

import javax.swing.event.*;
import javax.swing.table.*;

/**
 * @author nado18@gmail.com, on Jul 15, 2007
 */
public abstract class MyTableModel<I extends Comparable<I>> implements TableModel
{
	private String[] colNames;
	private Vector<TableModelListener> listeners =
			new Vector<TableModelListener>();
	private Vector<I> all = new Vector<I>();
	
	public MyTableModel(String... columnNames)
	{
		colNames = columnNames;
	}
	
	protected void notifyListeners()
	{
		TableModelEvent tme = new TableModelEvent( this, 0, getRowCount() );
		for (TableModelListener tml : listeners)
		{
			tml.tableChanged( tme );
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#addTableModelListener(javax.swing.event.TableModelListener)
	 */
	public void addTableModelListener( TableModelListener l )
	{
		listeners.add( l );
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#removeTableModelListener(javax.swing.event.TableModelListener)
	 */
	public void removeTableModelListener( TableModelListener l )
	{
		listeners.remove( l );
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	public int getRowCount()
	{
		return all.size();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#isCellEditable(int, int)
	 */
	public boolean isCellEditable( int rowIndex, int columnIndex )
	{
		return false;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
	 */
	public void setValueAt( Object value, int rowIndex, int columnIndex )
	{
		// In this default implementation cells are not editable.
	}
	
	public void setItems( Collection<I> items )
	{
		all.clear();
		all.addAll( items );
		
		Collections.sort( all );
		
		notifyListeners();
	}
	
	protected boolean isValidRow( int row )
	{
		return row >= 0 && row < getRowCount();
	}
	
	public I getItemAt( int row )
	{
		if (isValidRow( row ))
		{
			return all.elementAt( row );
		}
		return null;
	}
	
	protected boolean isValidColumn( int col )
	{
		return col >= 0 && col < getColumnCount();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	public int getColumnCount()
	{
		return colNames.length;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnName(int)
	 */
	public String getColumnName( int columnIndex )
	{
		if (isValidColumn( columnIndex ))
		{
			return colNames[columnIndex];
		}
		else
		{
			return null;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnClass(int)
	 */
	public Class<?> getColumnClass( int columnIndex )
	{
		if (isValidCell( 0, columnIndex ))
		{
			Object value = getValueAt( 0, columnIndex );
			Class<?> theClass = value.getClass();
			return theClass;
		}
		else
		{
			return null;
		}
	}
	
	protected boolean isValidCell( int row, int col )
	{
		return isValidColumn( col ) && isValidRow( row );
	}
	
}
